/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.service;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xstgongyi.eduaid.dao.StudentAidExportMapper;
import org.xstgongyi.eduaid.dataobject.GradeDO;
import org.xstgongyi.eduaid.dataobject.StudentAidExportDTO;
import org.xstgongyi.eduaid.manager.GradeManager;

import java.util.*;

/**
 * @author oldcrane
 * @created 2020-06-09
 */
@Service
public class StudentAidExportServiceImpl implements StudentAidExportService {

    private static final String STUDENT_CONTACT = "STUDENTCONTACTINFO";
    private static final String STUDENT_BANCK_ACCOUNT = "STUDENTBANKACCOUNT";
    private static final String SPONOR_INFO = "SPONSORCONTACTINFO";
    private static final String FEEDBACK_PROGRESS = "FEEDBACKPROGRESS";

    @Autowired
    private StudentAidExportMapper exportDao;

    @Autowired
    private GradeManager gradeManager;

    private List<StudentAidExportDTO> retrieveAidData(Integer projectId, Integer semesterId) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("offset", 0);
        condition.put("limit", 9999);
        condition.put("projectId", projectId);
        condition.put("semesterId", semesterId);
        return exportDao.listAids(condition);
    }

    @Override
    public HSSFWorkbook expoertAidsToExcel(Integer projectId, Integer semesterId, Set<String> options) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("资助名单");
        int rowNum = 0;
        // 添加标题行
        addRow(sheet, assembleLabels(options), rowNum);
        rowNum++;

        // 获取并添加资助记录
        List<StudentAidExportDTO> data = retrieveAidData(projectId, semesterId);
        for (StudentAidExportDTO aid : data) {
            addAidRow(aid, options, sheet, rowNum);
            rowNum++;
        }

        return workbook;
    }

    private List<String> assembleLabels(Set<String> options) {
        List<String> labels = new ArrayList<>(32);
        labels.add("编号");
        labels.add("姓名");
        labels.add("金额");
        labels.add("性别");
        labels.add("学校");
        labels.add("年级");
        if (options.contains(STUDENT_CONTACT)) {
            labels.add("学生电话");
            labels.add("其他联系方式");
        }
        if (options.contains(STUDENT_BANCK_ACCOUNT)) {
            labels.add("银行账户");
            labels.add("关系");
            labels.add("银行卡号");
            labels.add("开户行");
        }
        labels.add("志愿者");
        labels.add("资助人");
        if (options.contains(SPONOR_INFO)) {
            labels.add("资助人性别");
            labels.add("资助人电话");
            labels.add("资助人微信");
            labels.add("资助人邮箱");
            labels.add("资助需求和备注");
        }
        labels.add("资助进展");
        labels.add("募捐进展");
        labels.add("备注");
        if (options.contains(FEEDBACK_PROGRESS)) {
            labels.add("助学金收据");
            labels.add("学期小结");
            labels.add("收据反馈给资助人");
            labels.add("小结反馈给资助人");
        }
        return labels;
    }

    private void addAidRow(StudentAidExportDTO item, Set<String> options, HSSFSheet sheet, int rowNum) {
        HSSFRow row = sheet.createRow(rowNum);
        int index = 0;
        addTextCell(row, index, item.getStudentSn());
        index++;
        addTextCell(row, index, item.getStudentName());
        index++;
        addIntCell(row, index, item.getAidAmount());
        index++;
        addTextCell(row, index, item.getStudentGenderStr());
        index++;
        addTextCell(row, index, item.getSchool());
        index++;
        GradeDO grade = null != item.getGradeId() ? gradeManager.getById(item.getGradeId()) : null;
        addTextCell(row, index, grade.getName());
        index++;

        if (options.contains(STUDENT_CONTACT)) {
            addTextCell(row, index, item.getStudentPhone());
            index++;
            addTextCell(row, index, item.getStudentOtherContact());
            index++;
        }
        if (options.contains(STUDENT_BANCK_ACCOUNT)) {
            addTextCell(row, index, item.getBankAccountName());
            index++;
            addTextCell(row, index, item.getBankAccountRelation());
            index++;
            addTextCell(row, index, item.getBankAccountNumber());
            index++;
            addTextCell(row, index, item.getBankName());
            index++;
        }
        addTextCell(row, index, item.getVolunteerName());
        index++;
        addTextCell(row, index, item.getSponsorName());
        index++;
        if (options.contains(SPONOR_INFO)) {
            addTextCell(row, index, item.getSponsorGenderStr());
            index++;
            addTextCell(row, index, item.getSponsorPhone());
            index++;
            addTextCell(row, index, item.getSponsorWechat());
            index++;
            addTextCell(row, index, item.getSponsorEmail());
            index++;
            addTextCell(row, index, item.getSponsorRemark());
            index++;
        }
        addTextCell(row, index, item.getAidStatusStr());
        index++;
        addTextCell(row, index, item.getPairStatusStr());
        index++;
        addTextCell(row, index, item.getAidRemark());
        index++;
        if (options.contains(FEEDBACK_PROGRESS)) {
            addTextCell(row, index, item.getReceiptStatusStr());
            index++;
            addTextCell(row, index, item.getSummaryStatusStr());
            index++;
            addTextCell(row, index, item.getReceiptFeedbackStatusStr());
            index++;
            addTextCell(row, index, item.getSummaryFeedbackStatusStr());
            index++;
        }
    }

    private void addTextCell(HSSFRow row, int colNum, Object value) {
        String str = null == value ? "" : value.toString();
        row.createCell(colNum, CellType.STRING).setCellValue(str);
    }

    private void addIntCell(HSSFRow row, int colNum, Integer value) {
        row.createCell(colNum, CellType.NUMERIC).setCellValue(value);
    }

    private void addRow(HSSFSheet sheet, List<String> data, int rowNum) {
        HSSFRow row = sheet.createRow(rowNum);
        int index = 0;
        for (String str : data) {
            row.createCell(index).setCellValue(str);
            index++;
        }
    }
}
